import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { getItem } from '@/utils/storage'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/m',
    name: 'm',
    component: () => import('@/views/sms-login/sms-login.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home/home.vue'),
    redirect: '/home-page',
    children: [
      {
        path: '/home-page', // 首页
        name: 'home-page',
        component: () => import('@/views/home/home-page/home-page.vue')
      },
      {
        path: '/initiator-sign', // 合同
        name: 'initiator-sign',
        component: () =>
          import('@/views/home/initiator-sign/initiator-sign.vue')
      },
      {
        path: '/personal-center', // 个人信息
        name: 'personal-center',
        component: () =>
          import('@/views/home/personal-center/personal-center.vue')
      }
    ]
  },
  {
    path: '/check-contract', // 查看合同
    name: 'check-contract',
    component: () => import('@/views/check-contract/check-contract.vue')
  },
  {
    path: '/drag-sign-contract', // 拖拽签署合同
    name: 'drag-sign-contract',
    component: () => import('@/views/drag-sign-contract/drag-sign-contract.vue')
  },
  {
    path: '/fixed-sign-contract', // 固定位置签署合同
    name: 'fixed-sign-contract',
    component: () =>
      import('@/views/fixed-sign-contract/fixed-sign-contract.vue')
  },
  {
    path: '/painting-signature', // 绘画签名
    name: 'painting-signature',
    component: () => import('@/views/painting-signature/painting-signature.vue')
  },
  {
    path: '/contract-sign-success', // 合同签署成功
    name: 'contract-sign-success',
    component: () =>
      import('@/views/contract-sign-success/contract-sign-success.vue')
  },
  {
    path: '/contract-file-details', // 合同文件详情
    name: 'contract-file-details',
    component: () =>
      import('@/views/contract-file-details/contract-file-details.vue')
  },
  {
    path: '/real-name-mode', // 选择实名方式
    name: 'real-name-mode',
    component: () => import('@/views/real-name-mode/real-name-mode.vue')
  },
  {
    path: '/real-name', // 实名方式
    name: 'real-name',
    component: () => import('@/views/real-name/real-name.vue')
  },
  {
    path: '/real-face-success', // 实名成功
    name: 'real-face-success',
    component: () => import('@/views/real-face-success/real-face-success.vue')
  },
  {
    path: '/seal-signing-admin', // 印章签名管理
    name: 'seal-signing-admin',
    component: () => import('@/views/seal-signing-admin/seal-signing-admin.vue')
  },
  {
    path: '/approver-contract', // 审批合同
    name: 'approver-contract',
    component: () => import('@/views/approver-contract/approver-contract.vue')
  },
  {
    path: '/:pathMatch(.*)*', // 当去往没有注册过的页面就显示本页面
    name: 'not-found',
    component: () => import('@/views/not-found/not-found.vue')
  }
]

const router = createRouter({
  history: createWebHistory(), // createWebHistory createWebHashHistory
  routes
})

router.beforeEach((to) => {
  if (to.path !== '/login' && to.path !== '/m') {
    const token = getItem('token')
    if (!token) {
      return '/login'
    }
  }
})

export default router
